using System;
using System.Linq;
using System.Windows.Input;
using System.Windows.Media;
using TheGameClient.Processes;
using Waaahsabi.Controls.FileSystem;

namespace TheGameClient
{
	public class ProgramDigitizeRetina : ProgramBase
	{
		public override ulong InterfaceId
		{
			get { return new Guid("C13FB98E-4FC0-4CE9-92C3-F306C9283213").ToLong(); }
		}

		public override string ProcessName
		{
			get { return "Retinal digitizer"; }
		}

		protected override bool OnlyLocal
		{
			get { return true; }
		}

		protected override string SuccessStatus
		{
			get { return "Process completed."; }
		}

		protected override string FailureStatus
		{
			get { return "Process failed."; }
		}

		protected override string DropErrorStatus
		{
			get { return "Invalid drop target. Program can only be executed locally."; }
		}

		public override void Initialize()
		{
			Control.StatusText = "Drag into memory bank window to generate file.";
			base.Initialize();
		}

		protected override void OnDrop(object sender, MouseButtonEventArgs e)
		{
			if (State == ProgramState.Busy)
				return;
			if (TopmostWindow == null)
				return;

			bool isLocal = false;

			//the file container will either be the local file manager, or the generic one in a system connection.
			//so we check now if there is a GenericSystemInterface control inside the window we found:
			GenericSystemInterface gsi =
				VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), TopmostWindow).OfType<GenericSystemInterface>().
					FirstOrDefault();
			if (gsi == null)
				//no generic system interface found, thus we are either in a local filemanager, or something is wrong
			{
				GatewayFileManager gfm =
					VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), TopmostWindow).OfType<GatewayFileManager>().
						FirstOrDefault();
				if (gfm == null)
					return;
				isLocal = true;
			}

			if (!isLocal)
			{
				SourceConnectionId = gsi.ConnectionId;
			}

			Container =
				VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), TopmostWindow).OfType<IFileManager>().
					FirstOrDefault();
			if (Container != null)
			{
				//assign source file Id, because it is essentially a Guid,Guid signature for this operation, even though the 2nd Guid is a target directory id.
				SourceFileId = Container.DirectoryId;
				Control.Cursor = Cursors.Wait;
				Control.ShowProgressAnimation(Speed);
				Control.StatusText = "Generating...";
				State = ProgramState.Busy;
			}
		}
	}

	public class ProgramDigitizeRetinaFactory : UiFactoryBase
	{
		public override InterfaceCategory UiCategory
		{
			get { return InterfaceCategory.Program; }
		}

		public override IProcess CreateProgram()
		{
			return new ProgramDigitizeRetina();
		}
	}
}